<?php
//index SERVER

	//include config and classe
	include("config.php");
	include("class_user.php");
	
	session_start();
	
	//connect database
	$link=mysql_connect(DATABASE_ADDRESS, DATABASE_USER, DATABASE_PASS);
	mysql_select_db(DATABASE_NAME);
	
	if (isset($_GET['page']) and $_GET['page']=="rss"){
		include("rss.php");
		exit();
	}
	//instancier l'utilisateur
	if (isset($_SESSION['active']) and $_SESSION['active'] == 1)
	{
		$user = &$_SESSION['user'];
	}
	else
	{
		$user = new user();
		$_SESSION['user'] = $user;
		$_SESSION['active'] = 1;
	}

	//include fonction
	include("fonction.php");
	
	//identifier l'utilisateur
	$user->check_cookie();
	if (isset($_POST['hidden']) and $_POST['hidden']=="user_login"){
		if (! $user->check_password($_POST['user_login'],$_POST['user_passwd'])){
			$_SESSION['error'] = "Login/password error";
			//log
			add_log("bad password for ".mysql_escape_string($_POST['user_login']));
		}
		else{
			add_log("connection of ".mysql_escape_string($_POST['user_login']));
		}
	}	
	//deconnecter l'utilisateur
	if (isset($_GET['disconnect']) and $_GET['disconnect']=="ok" ){
		disconnect_user();	
	}

	
	
	
//authentification de l'utilisateur obligatoire
if($user->access != 0){	
	
	//gestion de l'update des films
	if (isset($_GET['update']) and $_GET['update']=="ok"){
		refresh_server($_GET['server']);
		
	}
	
   //fonction de ping
	if (isset($_GET['ping']) and $_GET['ping']!="" ){
		print check_serv_up($_GET['ping']);exit();
	}
	
	//fonction de download
	if (isset($_GET['film']) and $_GET['film']!="" and isset($_GET['idserver']) ){
		$array = get_id($_GET['idserver'],$user->login,$_GET['film']);
		send_id($array);
		exit();
	}
	
}		
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?php echo $html_title ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link rel="stylesheet" href="style.css" type="text/css" />
	<link rel="alternate" type="application/rss+xml" href="index.php?page=rss" />
	<link rel="icon" type="image/png" href="img/favicon.ico" />
	<script type="text/javascript" src="fonction.js"></script>
	<script src="js/prototype.js" type="text/javascript"></script>
 	<script src="js/scriptaculous.js?load=effects" type="text/javascript"></script>
	 
 	
</head>
<body>

	<?php
	//authentification de l'utilisateur
		if($user->access == 0){
	?>
	<div id="total">
	<form action="index.php" name="identification" method="post" >
	<table>
	<caption>Search Server</caption>
	<tr>
	<td>Login</td><td><input type = "text" name="user_login" /></td>
	</tr>
	<tr>
	<td>Password</td><td><input type="password" name="user_passwd" /></td>
	</tr>
	<tr>
	<td align="center" width="30px" colspan ="2"><input type="hidden" name="hidden" value="user_login" /><input type="submit" value="Connect" /></td>
	</tr>
	</table>
	</form>
	</div>
	<script language="javascript">
    	document.identification.user_login.focus();
	</script>
	
	<?php 		
		}
	//si user authentifier	
		else{		
	?>
	<div id="total2">
	<!-- Banniere -->
	<div id="div_banniere">
	
	</div>
	<!-- Fin de la Bannieres -->
	
	
	<!-- Info user -->
	<div id="info_user">
	
	</div>
	<!-- End info user -->
	

	
	<?php
	//gestion des differentes pages
	print "<div id=\"contain\">";
			if ($_GET['page']=="admin"){
				include('page_admin.php');
			}
			elseif ($_GET['page']=="films" or $_GET['page']=="other" or $_GET['page']=="search"){
					include('page_file.php');
				}
			elseif ($_GET['page']=="user"){
					include('page_user.php');
				}
			else{
				include('page_acceuil.php');
			}
	print "</div>";	
	?>
	
	
	<!-- MENU -->
	<div id="search_menu">
	
	<div id="menu1">
		<a href="index.php">Acceuil</a><br />
		<?php if($user->access == 2){ ?><a href="index.php?page=admin">Admin</a><br /> <?php } ?>
		<a href="index.php?page=films&server=choice">Films</a><br />
		<a href="index.php?page=user">User settings</a><br />
		<a href="index.php?page=other&server=choice">Other file</a>
	</div>
	
	<div id="menu2">
		User : <?php print $user->login; ?><br />
		<a href="index.php?disconnect=ok" >disconnect</a><br />
	</div>
	
	<div id="menu3">
		<form action="index.php" name="search" method="get" >
		<span>Recherche</span><br />
		<input type="text" name="file" value="search" onkeypress="vide('file')" onclick="vide('file')" style="width: 120px;" /><br />
		<input type="hidden" name="page" value="search" />
		
		<select name="server" style="width: 100px;">
			<option value="allserver">All server</option>
		<?php 
		$sql_query=mysql_query("SELECT name FROM ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
			while ($query = mysql_fetch_array($sql_query) ){
		?>
			<option value="<?php print $query['name'] ?>"><?php print $query['name'] ?></option>
		<?php } ?>
		</select>
		<input type="submit" value="Ok" id="searchsubmit" style="width: 26px;" />
		</form>
	</div>

	</div>

	<!-- END MENU -->
	<div id ="div_footer">
	<p>Pour toutes les remarques contactez moi</p>
	<a href="mailto:ghostloic@hotmail.com?subject=Search">PHP and CSS By Bewiwi</a>
	</div>
	<script language="javascript">
    	document.search.file.focus();
	</script>
	<?php
	
		}
	?>
	

	
	<?php
		
		//affichage des erreurs
		if (isset($_SESSION['error']) and $_SESSION['error'] != ""){
			echo "<div id=\"div_error\"> ERROR : ".$_SESSION['error']."</div>";
			$_SESSION['error'] = "";
		}
	?>
	</div>
</body>
</html>
	<?php mysql_close($link)?>